﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;
using DevExpress.XtraTabbedMdi;
using Shell.Events;
using Shell.Properties;

namespace Shell
{
    public partial class Shell : RibbonForm
    {
        readonly HashSet<IViewFactory> m_OpenedViewFactories = new HashSet<IViewFactory>();

        public string ApplicationName { get; set; }

        public Shell()
        {
            InitializeComponent();
            InitSkinGallery();
            NavigationGroups = new List<INavGroup>();
            iNew.ItemClick += OnNewItemClick;
            iClose.ItemClick += OnCloseItemClick;
            iSave.ItemClick += OnSaveItemClicked;
            iExit.ItemClick += OnExitItemClicked;
            iRefresh.ItemClick += OnRefresh;
            iDelete.ItemClick += OnDelete;
            iFind.ItemClick += OnFindItemClicked;

        }

        

        private void OnDelete(object sender, ItemClickEventArgs e)
        {
            var ok = ShowQuestion("Da li želite da obrišete dokument?");
            if (ok)
            {
                var view = ActiveView;
                if (view != null)
                {
                    view.OnDelete();
                }
            }
        }


        private void OnRefresh(object sender, ItemClickEventArgs e)
        {
            try
            {
                Busy();
                var view = ActiveView;
                if (view != null)
                {
                    view.OnRefresh();
                }

            }
            catch (Exception exc)
            {
                ShowError(exc.Message);
            }
            finally
            {
                NotBusy();
            }
        }

        private void OnExitItemClicked(object sender, ItemClickEventArgs e)
        {
            var ok = ShowQuestion("Da li želite da napustite aplikaciju?");
            if (ok)
            {
                Close();
            }
         }

        private void OnFindItemClicked(object sender, ItemClickEventArgs e)
        {
            var view = ActiveView;
            if (view != null)
            {
                view.OnSearch();
            }
        }

        private bool ShowQuestion(string message)
        {
            return DialogResult.Yes ==
                   XtraMessageBox.Show(this, message, ApplicationName, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        }

        private void ShowError(string message)
        {
            XtraMessageBox.Show(this, message, ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void ShowWarning(string message)
        {
            XtraMessageBox.Show(this, message, ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void ShowInfo(string message)
        {
            XtraMessageBox.Show(this, message, ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void OnSaveItemClicked(object sender, ItemClickEventArgs e)
        {
            try
            {
                Busy();
                var view = ActiveView;
                if (view != null)
                {
                    view.OnSave();
                }
 
            } catch(Exception exc)
            {
                ShowError(exc.Message);
            } finally
            {
                NotBusy();
            }
            
        }

        private void NotBusy()
        {
            Cursor = Cursors.Default;
        }

        private void Busy()
        {
            Cursor = Cursors.WaitCursor;
        }

        private void OnCloseItemClick(object sender, ItemClickEventArgs e)
        {
            var view = ActiveView;
            if (view != null)
            {
                view.Close();
            }
        }

        private void OnNewItemClick(object sender, ItemClickEventArgs e)
        {
            var view = ActiveView;
            if (view != null)
            {
                view.OnNew();
            }
        }

        void InitSkinGallery()
        {
            SkinHelper.InitSkinGallery(rgbiSkins, true);
        }

        public void AddGroup(INavGroup group)
        {
            NavigationGroups.Add(group);
            var navBarGroup = new NavBarGroup(group.Caption){LargeImage = group.GroupIcon};
            foreach(var viewFactory in group.ViewFactories)
            {
                var navBarItem = new NavBarItem(viewFactory.MenuCaption){SmallImage = viewFactory.NavigationIcon, Tag = viewFactory};
                navBarGroup.ItemLinks.Add(navBarItem);
                navBarItem.LinkPressed += OnNavigationLinkPressed;
            }
            navBarControl.Groups.Add(navBarGroup);
            navBarControl.ActiveGroup = navBarGroup;
        }

        private void OnNavigationLinkPressed(object sender, NavBarLinkEventArgs e)
        {
            var viewFactory = (IViewFactory)e.Link.Item.Tag;
            if (m_OpenedViewFactories.Add(viewFactory))
            {
                var view = viewFactory.Create();
                view.MdiParent = this;
                view.StatusChanged += OnViewStatusChanged;
                view.AlertChanged += OnViewAlertChanged;
                view.Closed += (s, e1) =>
                {
                    m_OpenedViewFactories.Remove(viewFactory);
                    view.StatusChanged -= OnViewStatusChanged;
                    view.AlertChanged -= OnViewAlertChanged;
                };
                view.Show();
            } else
            {
                foreach(XtraMdiTabPage page in xtraTabbedMdiManager1.Pages)
                {
                    if (((IView)page.MdiChild).Id == viewFactory.Id)
                    {
                        xtraTabbedMdiManager1.SelectedPage = page;
                    }
                }
            }
        }

        private void OnViewAlertChanged(object sender, AlertEventArgs e)
        {
            var icon = e.Icon ?? Resources.info_balloon;
            alertControl1.Show(this, ApplicationName, e.Message, icon);
        }

        public IView ActiveView
        {
            get
            {
                var selectedPage = xtraTabbedMdiManager1.SelectedPage;
                if (selectedPage == null) return null;
                return selectedPage.MdiChild as IView;
            }
        }

        private void OnViewStatusChanged(object sender, EventArg<string> e)
        {
            siStatus.Caption = e.Argument;
        }

        private IList<INavGroup> NavigationGroups { get; set; }

    }
}